package com.peng.spring.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 1.创建IOC容器：springframework会将创建好的对象放入IOC容器中
 * 2.获取对象：从IOC容器中获取已经创建好的对象
 * 3.调用对象方法
 * @Author peng
 * @Date 2024-04-16 10:31
 */
public class TestUser {
    public static void main(String[] args) {
        //使用注解方式创建对象
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext("com.peng.spring.example");//指定以注解方式创建对象
        //ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//指定以xml方式创建对象
        //ApplicationContext applicationContext = new FileSystemXmlApplicationContext("file:D:/applicationContext.xml");//指定以xml方式创建对象
        User user = applicationContext.getBean(User.class);
        user.say();

        //使用xml配置方式创建对象
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("bean.xml");
        UserXml userXml = (UserXml) classPathXmlApplicationContext.getBean("userXml");
        userXml.say();
    }
}
